package com.itheima.semaphore;

import java.util.concurrent.Semaphore;

public class Test {
    public static void main(String[] args) {
//        请使用Semaphore编写一个程序，实现以下效果：有10名游客要参观展览室，
// 而“展览室”同时只允许最多“三个游客”参观，每个游客参观时间2秒
        Semaphore semaphore = new Semaphore(3);
        for (int i = 1; i <= 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        semaphore.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"正在参观");
                    try {
                        Thread.sleep(2000);
                        System.out.println(Thread.currentThread().getName()+"结束参观");
                        semaphore.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
